home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / monform.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  6.7 KB  |  246 lines

  1. //----------------------------------------------------------------------------
  2. //Borland C++Builder
  3. //Copyright (c) 1987, 1998 Borland International Inc. All Rights Reserved.
  4. //----------------------------------------------------------------------------
  5. // Interprocess Communication Demo
  6. //
  7. //  This program along with the Client.dpr project, demonstrate a number
  8. //  of topics in Win32 programming. Threads, Events, Mutexes, and Shared
  9. //  memory are all used to provide communication between this monitor and
  10. //  it's clients, see IPCThrd.pas.
  11. //
  12. //  To Run, compile this project and the Client.cpp project.  Run one
  13. //  instance of the monitor and then run several instances of the client.
  14. //  You can switch between clients by clicking on the Client's window or
  15. //  by selecting it from the Client menu in the monitor.
  16. //
  17. //  Topics Covered:
  18. //
  19. //    Interprocess Communication
  20. //    Threads
  21. //    Events
  22. //    Mutexes
  23. //    Shared Memory
  24. //    Single instance EXE.
  25. //
  26. //---------------------------------------------------------------------------
  27. #include "Monform.h"
  28. #include "TrcView.h"
  29. #include "About.h"
  30. //---------------------------------------------------------------------------
  31. #pragma resource "*.dfm"
  32.  
  33. TMonitorForm *MonitorForm;
  34. __fastcall  TMonitorForm::TMonitorForm(TComponent *Owner):TForm(Owner)
  35. {
  36. }
  37.  
  38. /* Private Routines */
  39.  
  40. void __fastcall TMonitorForm::ClearLabels()
  41. {
  42.   int Index;
  43.  
  44.   for (Index = cfError; Index <= cfAttach; Index++)
  45.   {
  46.     TraceLabels[Index].YLabel->Caption = 0;
  47.     TraceLabels[Index].XLabel->Caption = 0;
  48.   }
  49. }
  50.  
  51. void __fastcall TMonitorForm::OnConnect(TIPCThread* Sender, Boolean Connecting)
  52. {
  53.   if (Connecting)
  54.   {
  55.     FStatusText = IPCMonitor->ClientName;
  56.     SignalClientStatus();
  57.   }
  58.   else
  59.     FStatusText = "No Client";
  60.   PostMessage(Handle, WM_UPDATESTATUS, 0, 0);
  61. }
  62.  
  63. /* When a client starts or stops we need to update the client menu.
  64.   We do this by posting a message to the Monitor Form, which in turn causes
  65.   the UpdateClientMenu method to be invoked.  We use this approach, rather than
  66.   calling UpdateClientMenu directly because this code is not being executed
  67.   by the main thread, but rather by the thread used in the TMonitorThread
  68.   class.  We could also have used the TThread.Synchonize method, but since
  69.   there is no need for the IPC thread to wait for the monitor to update
  70.   the menu, this approach is more effecient. */
  71.  
  72. void __fastcall TMonitorForm::OnDirectoryUpdate(TIPCThread *Sender)
  73. {
  74.   PostMessage(Handle, WM_UPDATEMENU, 0, 0);
  75. }
  76.  
  77. /* This event is triggered when the client has new data for us.  As with
  78.   the OnDirectoryUpdate event above, we use PostMessage to get the main
  79.   thread to update the display. */
  80.  
  81. void __fastcall TMonitorForm::OnSignal(TIPCThread *Sender, const TEventData &Data)
  82. {
  83.   FTraceMsg.X = Data.X;
  84.   FTraceMsg.Y = Data.Y;
  85.   FTraceMsg.Flag = Data.Flag;
  86.   PostMessage(Handle,
  87.               WM_SETTRACEDATA,
  88.               FTraceMsg.X,
  89.               FTraceMsg.Y);
  90. }
  91.  
  92. void __fastcall TMonitorForm::SignalClientStatus()
  93. {
  94.   if (PauseButton->Down == true){
  95.     TClientFlags NoFlags;
  96.     IPCMonitor->SignalClient(NoFlags);
  97.   }
  98.   else
  99.     IPCMonitor->SignalClient(FClientData.Flags);
  100. }
  101.  
  102. void __fastcall TMonitorForm::UpdateTraceData(TWMTraceData* Msg)
  103. {
  104.    if (FClientData.Flags.Contains(FTraceMsg.Flag))
  105.     {
  106.       TraceLabels[FTraceMsg.Flag].XLabel->Caption = String((int)Msg->X);
  107.       TraceLabels[FTraceMsg.Flag].YLabel->Caption = String((int)Msg->Y);
  108.     }
  109. }
  110.  
  111. void __fastcall TMonitorForm::UpdateStatusBar(TMessage* Msg)
  112. {
  113.   StatusBar->SimpleText = FStatusText;
  114.   ClearLabels();
  115. }
  116.  
  117. void __fastcall TMonitorForm::UpdateClientMenu(TMessage* Msg)
  118. {
  119.   int  I;
  120.   int  ID;
  121.   TStringList* List = new TStringList();
  122.   TMenuItem*  mi;
  123.  
  124.   IPCMonitor->GetClientNames(List);
  125.   while (miClients->Count > 0) {
  126.     miClients->Delete(0);
  127.   }
  128.   if (List->Count == 0)
  129.     miClients->Add(NewItem("(None)", 0, False, False, NULL, 0, ""));
  130.   else
  131.     for(I=0; I < List->Count; I++)
  132.     {
  133.       ID = (int)List->Objects[I];
  134.       mi = NewItem(List->Strings[I], 0, False, True, ClientMenuClick, 0, "");
  135.       mi->Tag = ID;
  136.       mi->RadioItem = True;
  137.       mi->GroupIndex = 1;
  138.       miClients->Add(mi);
  139.     }
  140.   delete List;
  141. }
  142.  
  143. /* Event Handlers */
  144.  
  145. void __fastcall TMonitorForm::SetupLabelArray()
  146. {
  147.     TraceLabels[cfMouseMove].XLabel = MoveX;
  148.     TraceLabels[cfMouseMove].YLabel = MoveY;
  149.     TraceLabels[cfMouseDown].XLabel = DownX;
  150.     TraceLabels[cfMouseDown].YLabel = DownY;
  151.     TraceLabels[cfResize].XLabel = SizeX;
  152.     TraceLabels[cfResize].YLabel = SizeY;
  153. }
  154.  
  155. void __fastcall TMonitorForm::FormCreate(TObject* Sender)
  156. {
  157.   IPCMonitor = new TIPCMonitor((int)Application->Handle, "Monitor");
  158.   IPCMonitor->OnSignal = OnSignal;
  159.   IPCMonitor->OnConnect = OnConnect;
  160.   IPCMonitor->OnDirectoryUpdate = OnDirectoryUpdate;
  161.   IPCMonitor->Activate();
  162.   OnDirectoryUpdate(NULL);
  163.   OnConnect(NULL, False);
  164.   FClientData.Flags << cfMouseMove << cfMouseDown << cfResize;
  165.   SetupLabelArray();
  166. }
  167.  
  168. void __fastcall TMonitorForm::FormDestroy( TObject* Sender)
  169. {
  170.   IPCMonitor->Free();
  171. }
  172.  
  173. void __fastcall TMonitorForm::ClientMenuClick( TObject* Sender)
  174. {
  175.   int  NewID;
  176.  
  177.   NewID = (int)((TMenuItem*)Sender)->Tag;
  178.   if (NewID != IPCMonitor->ClientID)
  179.     IPCMonitor->ClientID = NewID;
  180. }
  181.  
  182. void __fastcall TMonitorForm::miClientsClick(TObject* Sender)
  183. {
  184.  int  I;
  185.  
  186.   if (IPCMonitor->ClientID != 0)
  187.     for(I=0; I < miClients->Count; I++)
  188.       if (miClients->Items[I]->Tag == IPCMonitor->ClientID)
  189.       {
  190.          miClients->Items[I]->Checked = True;
  191.          break;
  192.       }
  193. }
  194.  
  195. void __fastcall TMonitorForm::SetTraceFlags( TObject* Sender)
  196. {
  197.  TClientFlag  F;
  198.  
  199.   F = (TClientFlag)((TCheckBox*)Sender)->Tag;
  200.   if (((TCheckBox*)Sender)->Checked)
  201.         FClientData.Flags << F;
  202.   else
  203.         FClientData.Flags >> F;
  204.  
  205.   SignalClientStatus();
  206. }
  207.  
  208. void __fastcall TMonitorForm::AutoClientSwitch1Click( TObject* Sender)
  209. {
  210.     ((TMenuItem*)Sender)->Checked = ! ((TMenuItem*)Sender)->Checked;
  211.     IPCMonitor->AutoSwitch = ((TMenuItem*)Sender)->Checked;
  212. }
  213.  
  214. void __fastcall TMonitorForm::miFileExitClick( TObject* Sender)
  215. {
  216.   Close();
  217. }
  218.  
  219. void __fastcall TMonitorForm::ShowTraceButtonClick( TObject* Sender)
  220. {
  221.   IPCMonitor->GetDebugInfo(TraceForm->TraceData->Items);
  222.   TraceForm->ShowModal();
  223. }
  224.  
  225. void __fastcall TMonitorForm::PauseButtonClick( TObject* Sender)
  226. {
  227.   SignalClientStatus();
  228. }
  229.  
  230. void __fastcall TMonitorForm::ClearButtonClick( TObject* Sender)
  231. {
  232.   IPCMonitor->ClearDebugInfo();
  233. }
  234.  
  235. void __fastcall TMonitorForm::ExitButtonClick( TObject* Sender)
  236. {
  237.   Close();
  238. }
  239.  
  240. void __fastcall TMonitorForm::About1Click( TObject* Sender)
  241. {
  242.   ShowAboutBox();
  243. }
  244.  
  245.  
  246.